package com.pshcp.service;

import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.framework.util.SystemUtil;
import com.github.pagehelper.PageHelper;
import com.pshcp.common.enums.CommonStatusEnum;
import com.pshcp.domain.dto.DoctorPatientGroupUserDTO;
import com.pshcp.domain.model.DoctorPatientGroupUser;
import com.pshcp.mapper.DoctorPatientGroupUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 医生的患者分组用户 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-02
 */
@Service
public class DoctorPatientGroupUserService {

    @Autowired
    private DoctorPatientGroupUserMapper doctorPatientGroupUserMapper;

    public void batchSaveDoctorPatientGroupUser(List<DoctorPatientGroupUser> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        Map<String, List<DoctorPatientGroupUser>> infoMap = new HashMap<>();
        infoMap.put("infoMap", list);
        doctorPatientGroupUserMapper.batchSaveDoctorPatientGroupUser(infoMap);
    }

    public List<DoctorPatientGroupUser> getDoctorPatientGroupUserList(Long doctorId, Long patientId, Long doctorPatientGroupId) {
        Example example = new Example(DoctorPatientGroupUser.class);
        Example.Criteria criteria = example.createCriteria();
        if (doctorId != null) {
            criteria.andEqualTo("doctorId", doctorId);
        }
        if (patientId != null) {
            criteria.andEqualTo("patientId", patientId);
        }
        if (doctorPatientGroupId != null) {
            criteria.andEqualTo("doctorPatientGroupId", doctorPatientGroupId);
        }
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        example.setOrderByClause(" created_time desc");
        List<DoctorPatientGroupUser> list = doctorPatientGroupUserMapper.selectByExample(example);
        return list;
    }

    public PageList<DoctorPatientGroupUser> list(DoctorPatientGroupUserDTO doctorPatientGroupUserDTO) {
        if (doctorPatientGroupUserDTO.getPageNum() != null && doctorPatientGroupUserDTO.getPageSize() != null) {
            PageHelper.startPage(doctorPatientGroupUserDTO.getPageNum(), doctorPatientGroupUserDTO.getPageSize());
        }
        Example example = new Example(DoctorPatientGroupUser.class);
        Example.Criteria criteria = example.createCriteria();
        if (doctorPatientGroupUserDTO.getStatus() != null) {
            criteria.andEqualTo("status", doctorPatientGroupUserDTO.getStatus());
        }
        example.setOrderByClause(" created_time desc");
        List<DoctorPatientGroupUser> list = doctorPatientGroupUserMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<DoctorPatientGroupUser> select(DoctorPatientGroupUser doctorPatientGroupUser) {
        return doctorPatientGroupUserMapper.select(doctorPatientGroupUser);
    }

    public int insert(DoctorPatientGroupUser doctorPatientGroupUser) {
        doctorPatientGroupUser.setId(SystemUtil.getId());
        doctorPatientGroupUser.setStatus(CommonStatusEnum.INIT.getCode());
        doctorPatientGroupUser.setCreatedTime(new Date());
        return doctorPatientGroupUserMapper.insert(doctorPatientGroupUser);
    }

    public int update(DoctorPatientGroupUser doctorPatientGroupUser) {
        doctorPatientGroupUser.setUpdateTime(new Date());
        return doctorPatientGroupUserMapper.updateByPrimaryKeySelective(doctorPatientGroupUser);
    }

    public DoctorPatientGroupUser findById(Long id) {
        return doctorPatientGroupUserMapper.selectByPrimaryKey(id);
    }

    public List<DoctorPatientGroupUser> findByIds(List<Long> ids) {
        Example example = new Example(DoctorPatientGroupUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return doctorPatientGroupUserMapper.selectByExample(example);
    }

    public int delById(Long id) {
        Example example = new Example(DoctorPatientGroupUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        DoctorPatientGroupUser doctorPatientGroupUser = new DoctorPatientGroupUser();
        doctorPatientGroupUser.setStatus(CommonStatusEnum.DEL.getCode());
        return doctorPatientGroupUserMapper.updateByExampleSelective(doctorPatientGroupUser, example);
    }

}
